//
//  OADLazyGetterViewController.m
//  OASDK_Works
//
//  Created by ChenJie on 3/20/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADLazyGetterViewController.h"
#import "OASDK.h"

@interface OADLazyGetterViewController ()
<
UITableViewDataSource,
UITableViewDelegate,
UITextFieldDelegate,
UITextViewDelegate
>

@property (nonatomic, strong) NSMutableArray *cells;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *arr1;
@property (nonatomic, strong) NSMutableArray *arr2;
@property (nonatomic, strong) NSMutableArray *arr3;
@property (nonatomic, strong) NSMutableArray *arr4;
@property (nonatomic, strong) NSMutableArray *arr5;
@property (nonatomic, strong) NSMutableArray *arr6;
@property (nonatomic, strong) NSMutableArray *arr7;
@property (nonatomic, strong) NSMutableArray *arr8;
@property (nonatomic, strong) NSMutableArray *arr9;
@property (nonatomic, strong) NSMutableArray *arr10;
@property (nonatomic, strong) NSMutableArray *arr11;

@property (nonatomic, strong) NSMutableDictionary *d1;

@property (nonatomic, strong) UIView *aView;
@property (nonatomic, strong) UILabel *label222;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIBarButtonItem *barButtonItem;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UITextField *textField2;
@property (nonatomic, strong) UITextView *textView;

@end

@implementation OADLazyGetterViewController

#pragma mark - ViewController lifecycle

- (instancetype)init {
    self = [super init];
    if (self) {
        [self buildDataSource];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSStringFromClass(self.class);
    
    [self.view addSubview:self.aView];
    [self.view addSubview:self.label222];
    [self.view addSubview:self.button];
    [self.view addSubview:self.imageView];
    [self.view addSubview:self.textField2];
    [self.view addSubview:self.textView];
    self.navigationItem.rightBarButtonItem = self.barButtonItem;
    
    
    [self.view addSubview:self.tableView];
    
    [self.arr1 addObject:@"11111"];
    NSLog(@"arr1 is: %@", self.arr1);
    
    [self.arr2 addObject:@"2222"];
    NSLog(@"arr2 is : %@", self.arr2);
    
    [self.arr3 addObject:@"3333"];
    NSLog(@"arr3 is: %@", self.arr3);
    
    [self.arr4 addObject:@"4444444"];
    NSLog(@"arr4 is: %@", self.arr4);
    
    [self.arr5 addObject:@"555"];
    NSLog(@"arr5 is: %@", self.arr5);
    
    [self.arr6 addObject:@"6666"];
    NSLog(@"arr6 is: %@", self.arr6);
    
//    NSLog(@"cells: %@", self.cells);
    
    self.d1[@"key3"] = @"33333";
    NSLog(@"d1 is: %@", self.d1);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    CGFloat topMark = 0;
    CGFloat marginV = 10;
    
    topMark = OA_NAVIGATION_BAR_BOTTOM;
    CGFloat viewDimension = 100;
    self.aView.frame = CGRectMake(0, topMark, viewDimension, viewDimension);
    topMark += viewDimension;
    
    topMark += marginV;
    CGFloat labelHeight = 44;
    self.label222.frame = CGRectMake(0, topMark, 300, labelHeight);
    topMark += labelHeight;
    
    topMark += marginV;
    CGFloat buttonHeihgt = 44;
    self.button.frame = CGRectMake(0, topMark, 300, buttonHeihgt);
    topMark += buttonHeihgt;
    
    topMark += marginV;
    CGFloat imageViewDimension = 100;
    self.imageView.frame = CGRectMake(0, topMark, 100, imageViewDimension);
    topMark += imageViewDimension;
    
    topMark += marginV;
    CGFloat textFieldHeight = 44;
    self.textField2.frame = CGRectMake(0, topMark, 300, textFieldHeight);
    topMark += textFieldHeight;
    
    topMark += marginV;
    CGFloat textViewHeight = 100;
    self.textView.frame = CGRectMake(0, topMark, 300, textViewHeight);
    topMark += textViewHeight;
    
    topMark += marginV;
//    topMark = OA_NAVIGATION_BAR_BOTTOM;
    CGFloat tableViewHeight = self.view.oa_height - topMark;
    self.tableView.frame = CGRectMake(0, topMark, self.view.oa_width, tableViewHeight);
    
    
    //
    self.aView.backgroundColor = OARandomColor();
    [self.view oa_showBounds];
    
//    self.tableView.frame = CGRectMake(0, topMark, viewSize.width, viewSize.height - topMark);
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - Public methods



#pragma mark - Private methods

- (void)buildDataSource {
    NSArray *cells = @[
        @{@"class": UITableViewCell.class,
          @"title": @"One",
          @"target": UIViewController.class,
        },
        @{@"class": UITableViewCell.class,
          @"title": @"Two",
          @"target": @"UIViewController",
        },
        @{@"title": @"Three",
        },
        @{@"class": UITableViewCell.class,
          @"title": @"Four",
          @"target": UIViewController.class,
        },
    ];
    
    [self.cells addObjectsFromArray:cells];
}

- (Class)cellClassAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *cellInfo = self.cells[indexPath.row];
    Class cellClass = cellInfo[@"class"];
    return cellClass ?: UITableViewCell.class;
}

- (NSString *)titleAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *cellInfo = self.cells[indexPath.row];
    return cellInfo[@"title"];
}

- (UIViewController *)targetViewControllerAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *cellInfo = self.cells[indexPath.row];
    Class targetVcClass = cellInfo[@"target"] ?: UIViewController.class;
    if ([targetVcClass isKindOfClass:NSString.class]) {
        targetVcClass = NSClassFromString((NSString *)targetVcClass);
    }
    
    UIViewController *targetVc = [[targetVcClass alloc] init];
    if ([targetVc isMemberOfClass:UIViewController.class]) {
        targetVc.view.backgroundColor = [UIColor whiteColor];
        targetVc.navigationItem.title = NSStringFromClass(targetVc.class);
    }
    
    return targetVc;
}

#pragma mark - Event response methods



#pragma mark - Deletate methods



#pragma mark - <UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cells.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Class cellClass = [self cellClassAtIndexPath:indexPath];
    NSString *cellIdentifier = NSStringFromClass(cellClass);
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    cell.textLabel.text = [self titleAtIndexPath:indexPath];
    
//    NSLog(@"%s", __PRETTY_FUNCTION__);
    
    return cell;
}

#pragma mark - <UITableViewDelegate>

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UIViewController *vc = [self targetViewControllerAtIndexPath:indexPath];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Setter



#pragma mark - Getter

//- (NSMutableArray *)arr2 {
//    if (!_arr2) {
//
//        NSMutableArray * (^initObject)(void) = ^NSMutableArray * (void){
//            return [NSMutableArray array];
//        };
//
//        void (^setupObject)(NSMutableArray *) = ^void (NSMutableArray *array) {
//            NSLog(@"do it...");
//        };
//
//        NSMutableArray *array = initObject();
//        setupObject(array);
//
//        _arr2 = array;
//
//    }
//    return _arr2;
//}

//OAGet(NSMutableArray, arr1) = ^NSMutableArray * (void) {
//    NSMutableArray *array = [NSMutableArray array];
//    [array addObject:@"01234567"];
//    return array;
//};
//
//OAGetNSMutableArray(cells) = ^(NSMutableArray *array){
//    [array addObject:@{
//        @"class": UITableViewCell.class,
//        @"title": @"first row",
//        @"target": OADLazyGetterViewController.class,
//    }];
//};
//
//OAGetNSMutableArray(arr2) = ^(NSMutableArray *array){
//    [array addObject:@"setup arr2"];
//};
//
//OAGetNSMutableDictionary(d1) = ^(NSMutableDictionary *dictionary){
//    dictionary[@"key1"] = @"111";
//    dictionary[@"key2"] = @2222;
//};
//
//OAGetUIView(aView) = ^(UIView *view){
//    view.layer.borderColor = OARandomColor().CGColor;
//    view.layer.borderWidth = 5;
//};
//
//OAGetUILabel(label222) = ^(UILabel *label){
//    label.text = @"label";
//};

- (NSMutableArray *)arr5 {
    if (!_arr5) {
        
        NSMutableArray * (^initObject)(void) = ^NSMutableArray * (void){
            return [NSMutableArray array];
        };
        
        void (^setupObject)(NSMutableArray *, id targetSelf) = ^void (NSMutableArray *array, id targetSelf) {
            UIButton *button  = [UIButton buttonWithType:UIButtonTypeSystem];
            [button addTarget:self
                       action:@selector(buttonDidClick:)
             forControlEvents:UIControlEventTouchUpInside];
        };
        
        NSMutableArray *array = initObject();
        setupObject(array, self);
        _arr5 = array;
    }
    return _arr5;
}

OAGet(NSMutableArray, arr1)

OAGet(NSMutableArray, arr2, ^NSMutableArray * (void){
    NSMutableArray *array = [NSMutableArray array];
    [array addObject:@"init arr2"];
    return array;
})

OAGetMutableArray(arr3)

OAGetMutableArray(arr4, ^(NSMutableArray *array){
    [array addObject:@"init arr4"];
})

OAGetMutableDictionary(d1, ^(NSMutableDictionary *dictionary){
    dictionary[@"key1"] = @"vvvvv";
})

OAGetMutableArray(cells, ^(NSMutableArray *array){
    [array addObject:(@{
        @"class": UITableViewCell.class,
        @"title": @"----",
        @"target": OADLazyGetterViewController.class,
    })];
})

OAGetView(aView, ^(UIView *view){
    view.layer.borderColor = OAColor(22ff00).CGColor;
    view.layer.borderWidth = 5;
})

OAGetLabel(label222, ^(UILabel *label){
    label.layer.borderColor = OARandomColor().CGColor;
    label.layer.borderWidth = 2;
    label.text = NSStringFromSelector(_cmd);
})

- (void)buttonDidClick:(UIButton *)button {
    NSLog(@"do it..");
}

OAGetButton(button, ^(UIButton *button){
    [button setTitle:@"new title" forState:UIControlStateNormal];
})

OAGetImageView(imageView, ^(UIImageView *imageView){
    imageView.layer.borderColor = OARandomColor().CGColor;
    imageView.layer.borderWidth = 5;
    imageView.image = [UIImage imageNamed:@"iconCompany"];
})

OAGetTextField(textField2, ^(UITextField *textField){
    textField.placeholder = @"sdfds";
})

OAGetTextView(textView, ^(UITextView *textView){
    textView.text = @"sdfsdf";
});

- (void)barButtonItemDidClick:(UIBarButtonItem *)buttonItem {
    NSLog(@"button item clicked...");
}

OAGetBarButtonItem(barButtonItem, ^(UIBarButtonItem *barButtonItem){
    [barButtonItem setTitle:@"test"];
})

OAGetTableView(tableView, ^(UITableView *tableView){
    tableView.dataSource = self;
    tableView.delegate = self;
});

#pragma mark - Memory management

- (void)dealloc {
}

@end
